package com.dingding.action;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.dingding.service.chatmessageservice.IChatMessageService;
import com.dingding.service.userorderservice.IUserOrderService;
import com.dingding.util.DataUtil;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 
 * @ClassName: BaseAction
 * @Description: 所有action的父类
 * @author cheshaowei
 * @date 2015-7-1 下午03:21:35
 * @version 叮叮搭车V2.0
 * 
 */

@SuppressWarnings("serial")
public abstract class BaseAction extends ActionSupport {

	private static final Logger log = Logger.getLogger(BaseAction.class);

	protected RequestDispatchProxy requestDispatchProxy;

	// 用户更订单临时表service(Service注入)
	protected IUserOrderService userOrderService;

	// 聊天记录Service对象
	protected IChatMessageService chatMessageService;

	/**
	 * 
	 * @Title: getClientData
	 * @Description:获取客户端请求数据
	 * @author cheshaowei
	 * @throws
	 */
	protected String getClientData() {
		String jsonData = DataUtil.getData();
		return jsonData;
	}

	/**
	 * @Title: write
	 * @Description:输出数据到客户端
	 * @author cheshaowei
	 * @throws
	 */
	protected void write(String text) {
		DataUtil.dataWrite(text);
	}

	/**
	 * 获取响应打印流
	 */
	protected PrintWriter getWriter() {

		PrintWriter printWriter = null;

		try {
			printWriter = ServletActionContext.getResponse().getWriter();
		} catch (IOException e) {
			log.error("获取打印流出现异常！", e);
		}

		return printWriter;

	}

	// 获取request输入流
	protected InputStream getRequestInputStream() {
		HttpServletRequest request = ServletActionContext.getRequest();
		InputStream inputStream = null;
		try {
			inputStream = request.getInputStream();
		} catch (IOException e) {
			log.error("获取输入流出错！BaseAction：getRequestInputStream()", e);
		}
		return inputStream;
	}

	// 返回给请求方字符串信息
	protected void renderText(String str) {

		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("UTF-8");

		PrintWriter writer = null;

		try {
			writer = response.getWriter();
			writer.print(str);
			writer.flush();
		} catch (IOException e) {
			log.error("响应" + str + "信息出错！BaseAction：renderText()", e);
		} finally {
			if (writer != null) {
				writer.close();
			}
		}
	}

	public RequestDispatchProxy getRequestDispatchProxy() {
		return requestDispatchProxy;
	}

	public void setRequestDispatchProxy(RequestDispatchProxy requestDispatchProxy) {
		this.requestDispatchProxy = requestDispatchProxy;
	}

	public IUserOrderService getUserOrderService() {
		return userOrderService;
	}

	public void setUserOrderService(IUserOrderService userOrderService) {
		this.userOrderService = userOrderService;
	}

	public IChatMessageService getChatMessageService() {
		return chatMessageService;
	}

	public void setChatMessageService(IChatMessageService chatMessageService) {
		this.chatMessageService = chatMessageService;
	}

}
